package Nucleo;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public final class ConexaoBanco {

	public java.sql.Connection conn = null;
    
	private String url = null;
    private String jdbcDriver = null;
    private String dataBaseName = null;
    private String dataBasePrefix = null;
    private String dataBasePort = null;
    private String hostName = null;
    private String userName = null;
    private String password = null;
	
    public ConexaoBanco(){
        jdbcDriver = "com.mysql.jdbc.Driver";
        hostName = "127.0.0.1";
        userName = "root";
        password = "vertrigo";
        dataBaseName = "locadora";
        dataBasePrefix = "jdbc:mysql://";
        dataBasePort = "3306";
		url = dataBasePrefix + hostName + ":" + dataBasePort + "/"
        	+ dataBaseName;
	}
	
	public java.sql.Connection getConnection() {
        try {
            if (conn == null) {
                Class.forName(jdbcDriver);
                conn = DriverManager.getConnection(url, userName, password);
            } else if (conn.isClosed()) {
                conn = null;
                return getConnection();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    
    public void closeConnection() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
